package controller;

import Interceptor.AdminCheckInterceptor;
import Interceptor.CategoryListInterceptor;
import Interceptor.NavInterceptor;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.LogKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import model.Category;
import model.Topic;

import java.util.List;

/**
 * Created by Administrator on 2018/5/9 0009.
 */
public class CategoryController extends Controller {

    public void list(){
      List<Category> categories = Category.dao.find("Select * From t_category");
      setAttr("categories",categories);
      renderFreeMarker("list.ftl");
    }
    @Before(AdminCheckInterceptor.class)
    public void add(){
      renderFreeMarker("add.ftl");
    }
    public void save(){
      String name = getPara("name");

      Category category = new Category();
      category.setName(name);
//        Category category = getModel(Category.class, "", true);
        boolean success = false;
      try{
      category.save();
      success = true;
      }catch (Exception e){
        LogKit.error("板块添加失败，原因："+e.getMessage());
     }
      renderHtml((success ? "添加成功": "添加失败") +
                "<br><a href='/category/list'>返回板块列表</a>");
    }
    @Before({CategoryListInterceptor.class, AdminCheckInterceptor.class})
    public void modify(){
      Integer id = getParaToInt(0,-1);
      Category category = Category.dao.findById(id);
      setAttr("category",category);
      renderFreeMarker("modify.ftl");
    }
    public void update(){
      Integer id = getParaToInt("id",-1);
        String name = getPara("name");
        boolean success = false;
        try {
            Db.update("UPDATE t_category SET name = ? WHERE id = ?",name,id);
            success = true;
        }catch (Exception e){
            LogKit.error("板块更新失败，原因："+e.getMessage());
        }

        renderHtml((success ? "修改成功": "修改失败") +
                "<br><a href='/category/list'>返回板块列表</a>");
    }
    @Before({CategoryListInterceptor.class, AdminCheckInterceptor.class})
    public void delete(){
        Integer id = getParaToInt(0,-1);
        boolean success = false;
        try {
            Category.dao.deleteById(id);
            success = true;
        }catch (Exception e){
            LogKit.error("板块删除失败，原因："+e.getMessage());
        }

        renderHtml((success ? "删除成功": "删除失败") +
                "<br><a href='/category/list'>返回板块列表</a>");
    }
    @Before(NavInterceptor.class)
    public void index(){
        renderHtml("yes");
        Integer categoryId = getParaToInt(0, -1);
        Integer pageNumber = getParaToInt("page", 1);
        String sqlSelect = "SELECT t_topic.*, t_user.nickname";


        String sqlExceptSelect ="FROM  t_topic INNER JOIN t_user ON t_topic.user_id = t_user.id WHERE category_id = ? ORDER BY pub_date DESC";

        Page<Topic> topicPage = Topic.dao.paginate(pageNumber, 5, sqlSelect, sqlExceptSelect, categoryId);
        setAttr("topicPage", topicPage);
        renderFreeMarker("topic-list.ftl");
    }

}
